package cn.itcast.service.system.impl;

import cn.itcast.dao.system.DeptDao;
import cn.itcast.domain.system.Dept;
import cn.itcast.service.system.DeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;


    @Override
    public PageInfo<Dept> findByPage(String companyId,int pageNum, int pageSize) {
        // 开始分页
        PageHelper.startPage(pageNum,pageSize);
        List<Dept> all = deptDao.findAll(companyId);
        // 调用dao，封装分页结果
        return new PageInfo<>(all);
    }

    @Override
    public List<Dept> findParent(String id, String companyId) {

        List<Dept> parent = deptDao.findParent(id, companyId);
        return parent;

    }

    @Override
    public Integer save(Dept dept) {
        dept.setId(UUID.randomUUID().toString());
        Integer save = deptDao.save(dept);
        return save;
    }


    @Override
    public List<Dept> findAll(String companyId){
        List<Dept> all = deptDao.findAll(companyId);
        return all;
    }

    @Override
    public Integer update(Dept dept) {
        if (dept.getParent().getId() == ""){
            dept.getParent().setId(null);
        }
        Integer update = deptDao.update(dept);
        return update;
    }

    @Override
    public Dept findById(String id) {
        Dept dept = deptDao.findById(id);
        return dept;
    }

    @Override
    public Integer delete(String id) {
        Integer delete = deptDao.delete(id);
        return delete;
    }

}
